package net

import (
	"net"

	"github.com/oschwald/geoip2-golang"
)

func NewIpGeo(path string) *ipGeo {
	db, err := geoip2.Open(path)
	if err != nil {
		panic(err)
	}
	defer func(db *geoip2.Reader) {
		err := db.Close()
		if err != nil {
			panic(err)
		}
	}(db)

	return &ipGeo{
		path: path,

		db: db,
	}
}

type ipGeo struct {
	path string

	db *geoip2.Reader
}

func (r *ipGeo) City(ipString string) *geoip2.City {
	ip := net.ParseIP(ipString)

	record, err := r.db.City(ip)
	if err != nil {
		panic(err)
	}

	return record
	// record.City.Names["pt-BR"]
	// if len(record.Subdivisions) > 0 {
	// 	record.Subdivisions[0].Names["en"]
	// }
	// record.Country.Names["zh"]
	// record.Country.IsoCode
	// record.Location.TimeZone
	// record.Location.Latitude
	// record.Location.Longitude
}
func (r *ipGeo) Country(ipString string) *geoip2.Country {
	ip := net.ParseIP(ipString)

	record, err := r.db.Country(ip)
	if err != nil {
		panic(err)
	}

	return record
}
func (r *ipGeo) Enterprise(ipString string) *geoip2.Enterprise {
	ip := net.ParseIP(ipString)

	record, err := r.db.Enterprise(ip)
	if err != nil {
		panic(err)
	}

	return record
}
func (r *ipGeo) AnonymousIP(ipString string) *geoip2.AnonymousIP {
	ip := net.ParseIP(ipString)

	record, err := r.db.AnonymousIP(ip)
	if err != nil {
		panic(err)
	}

	return record
}

// ASN
// ConnectionType
// Domain
// ISP
// Metadata
